
package com.hlkj.pay.controller.merchant;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.merchant.IMerchantReportAppService;
import com.hlkj.pay.app.merchant.impl.MerchantReportStatusProcessHandler;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.merchant.MerchantAppQueryDto;
import com.hlkj.pay.dto.merchant.report.*;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.service.merchant.IMerchantApplicationService;
import com.hlkj.pay.service.merchant.IMerchantReportService;
import com.hlkj.pay.vo.common.req.DeleteIdReq;
import com.hlkj.pay.vo.merchant.req.report.MerchantReportApproveReq;
import com.hlkj.pay.vo.merchant.req.report.MerchantReportInfoReq;
import com.hlkj.pay.vo.merchant.req.report.MerchantReportPageReq;
import com.hlkj.pay.vo.merchant.req.report.MerchantReportReq;
import com.hlkj.pay.vo.merchant.resp.report.MerchantReportInfoListResp;
import com.hlkj.pay.vo.merchant.resp.report.MerchantReportResp;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * @author HlpayTeam
 * @date 2024/09/12 11:32
 */
@Tag(name = "汇电通商户报备")
@RestController
@RequestMapping("/v1/merchant/report")
@RequiredArgsConstructor
public class MerchantReportController extends BaseController {

    private final IMerchantReportAppService merchantReportAppService;

    private final IMerchantReportService merchantReportService;
    private final MerchantReportStatusProcessHandler merchantReportStatusProcessHandler;
    private final IMerchantApplicationService merchantApplicationService;

    @Operation(summary = "汇电通保存草稿")
    @PostMapping("/save")
    CommonResult<Long> saveReport(@RequestBody MerchantReportReq merchantReportReq) {
        return merchantReportAppService.saveReport(merchantReportReq);
    }

    @Operation(summary = "汇电通获取签约链接")
    @GetMapping("/getSignUrl")
    CommonResult<String> getSignUrl(@RequestParam("id") Long id) {
        CommonResult<String> merchantReportRespCommonResult = merchantReportAppService.getSignUrl(id);
        return merchantReportRespCommonResult;
    }


    @Operation(summary = "汇电通提交报备")
    @PostMapping("/submit")
    CommonResult<Long> submitAccess(@RequestBody MerchantReportReq merchantReportReq) {
        // 前端提交的默认数据校验处理
        MerchantReportInfoReq merchantReportInfo = merchantReportReq.getMerchantReportInfo();
        Integer type = merchantReportInfo.getType();
        Integer contactType = merchantReportInfo.getContactType();
        Integer controllerType = merchantReportInfo.getControllerType();
        if (MerchantEnum.MERCHANT_TYPE.PERSONAL.getCode().equals(type)) {
            merchantReportInfo.setMerchantReportLicense(null);
        }
        if (CommonEnum.YES_NO_TYPE.YES.getCode().equals(contactType)) {
            merchantReportInfo.setMerchantReportContact(null);
        }
        if (CommonEnum.YES_NO_TYPE.YES.getCode().equals(controllerType)) {
            merchantReportInfo.setMerchantReportController(null);
        }
        CommonResult<MerchantReportDto> reportDtoCommonResult = merchantReportAppService.submitReport(merchantReportReq);
        if(!reportDtoCommonResult.isSuccess()){
            return CommonResult.error(reportDtoCommonResult.getCode(),reportDtoCommonResult.getMsg());
        }
        return CommonResult.success(reportDtoCommonResult.getData().getMerchantReportInfo().getId());
    }

    @Operation(summary = "汇电通报备详情")
    @GetMapping("/detail")
    CommonResult<MerchantReportResp> merchantReportInfoDetail(@RequestParam("id") Long id) {
        CommonResult<MerchantReportResp> merchantReportRespCommonResult = merchantReportAppService.merchantReportInfo(id);
        return merchantReportRespCommonResult;
    }
    @Operation(summary = "状态重跑 补偿接口 前端不用关注")
    @GetMapping("/process")
    CommonResult<Void> processAccessMessage(@RequestParam("id") Long id, @RequestParam("merchantAccessQuestType") Integer merchantAccessQuestType) {
        MerchantReportConsumerDto merchantReportConsumerDto=new MerchantReportConsumerDto();
        merchantReportConsumerDto.setReportId(id);
        merchantReportStatusProcessHandler.processReportMessage(merchantReportConsumerDto, merchantAccessQuestType);
        return CommonResult.success();
    }
    @Operation(summary = "汇电通报备短信发送")
    @PostMapping("/sms")
    CommonResult<Void> merchantReportSmsSend(@Validated @RequestBody MerchantReportSmsSendDto merchantReportSmsSendDto) {
        return merchantReportAppService.merchantReportSmsSend(merchantReportSmsSendDto);
    }
    @Operation(summary = "汇电通余额查询")
    @GetMapping("/balance")
    CommonResult<MerchantReportBalanceDto> merchantReportBalance(@RequestParam("id") Long id) {
        return merchantReportAppService.merchantReportBalance(id);
    }
    @Operation(summary = "汇电通资金调拨")
    @GetMapping("/allot")
    CommonResult<Void> merchantReportAllot(@RequestParam("id") Long id) {
        return merchantReportAppService.merchantReportAllot(id);
    }

    @Operation(summary = "汇电通报备短信验证")
    @PostMapping("/sms/code")
    CommonResult<Void> merchantReportSmsCode(@Validated @RequestBody MerchantReportSmsCodeDto merchantReportSmsCodeDto) {
        return merchantReportAppService.merchantReportSmsCode(merchantReportSmsCodeDto);
    }

    @Operation(summary = "汇电通报备审核")
    @PostMapping("/approve")
    CommonResult<Void> merchantReportInfoApprove(@Validated @RequestBody MerchantReportApproveReq merchantReportApproveReq) {
        return merchantReportAppService.platApprove(merchantReportApproveReq);
    }

    @Operation(summary = "汇电通报备删除")
    @DeleteMapping("")
    CommonResult<Void> delMerchantReportInfo(@Validated @RequestBody DeleteIdReq deleteIdReq) {
        return merchantReportAppService.delete(deleteIdReq.getId());
    }

    @Operation(summary = "汇电通报备列表")
    @GetMapping("")
    CommonResult<PageResult<MerchantReportInfoListResp>> merchantReportInfoPage(MerchantReportPageReq merchantReportInfoPageReq) {
        MerchantReportInfoPageQueryDto merchantReportInfoPageQueryDto = BeanUtil.copyProperties(merchantReportInfoPageReq, MerchantReportInfoPageQueryDto.class);
        processQueryDate(merchantReportInfoPageReq, merchantReportInfoPageQueryDto);
        merchantReportInfoPageQueryDto.setLikeSn(merchantReportInfoPageQueryDto.getSn());
        merchantReportInfoPageQueryDto.setSn(null);
        PageResult<MerchantReportInfoPageDto> page = merchantReportService.queryPage(merchantReportInfoPageQueryDto);
        List<MerchantReportInfoPageDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<MerchantReportInfoListResp> merchantReportInfoListResps = BeanUtil.copyToList(list, MerchantReportInfoListResp.class);
        for(MerchantReportInfoListResp merchantReportInfoListResp:merchantReportInfoListResps){
            MerchantAppQueryDto merchantAppQueryDto = new MerchantAppQueryDto();
            merchantAppQueryDto.setChannelCode(merchantReportInfoListResp.getChannelCode());
            merchantReportInfoListResp.setOutMerchantAppChannelList(merchantApplicationService.queryChannelInfoList(merchantAppQueryDto));
        }
        processProvideList(merchantReportInfoListResps);
        return buildPageResult(page, merchantReportInfoListResps);
    }



}
